﻿#include "GLLInc.h"
#include "GLLLevel.h"
#include "GLLObjSpcl.h"
#include "GLLTheSystem.h"

#ifdef GLL_OGRE

namespace GLL
{

	void Level::_OnLoadingBegin_spec(LevelThreadType tt)
	{
		//if(tt == TT_LOAD)
		{
			theWorld()->_root->getRenderSystem()->preExtraThreadsStarted();
		}
	}

	void Level::_OnLoadingEnd_spec(LevelThreadType tt)
	{
		//if(tt == TT_LOAD)
		{
			theWorld()->_root->getRenderSystem()->postExtraThreadsStarted();
		}
	}

	void Level::_OnLoadingThreadBegin_spec(LevelThreadType tt)
	{
		//放松，不要那么紧张
		theApp()->SetThisThreadPriority(OSApp::TP_LOW);

		//if(tt == TT_LOAD)
		{
			theWorld()->_root->getRenderSystem()->registerThread();
		}
	}

	void Level::_OnLoadingThreadEnd_spec(LevelThreadType tt)
	{
		//if(tt == TT_LOAD)
		{
			theWorld()->_root->getRenderSystem()->unregisterThread();
		}
	}

	Ogre::String _GetSMName( Level::ID id )
	{
		StringStream strm;
		strm << "OgreSM."<< id;
		return strm.str();
	}

	void Level::OnInit_spec( )
	{
		_smOgre = theWorld()->_root->createSceneManager(Ogre::ST_INTERIOR, _GetSMName(_id));
		//Ogre::SceneManager* sm = _root->createSceneManager(Ogre::ST_EXTERIOR_CLOSE, GetSMName(id));
		//Ogre::SceneManager* sm = _root->createSceneManager(Ogre::ST_GENERIC, GetSMName(id));
		if(!_smOgre) return;

		_node = _smOgre->getRootSceneNode();

		//_smOgre->setAmbientLight(Ogre::ColourValue::White);
		//SetShadowTech(id);

	}

	void Level::OnFinal_spec( )
	{
		theWorld()->_root->destroySceneManager(_smOgre);
	}

	void Level::SetShadowTech_spec()
	{
		//SetDefaultDirectionLight(id, true);

		//return;
		//Ogre::String shadow = GetCfgValueByKey("Shadow");
		//if(shadow != "true") return;

		//InitShadows(id);
		//gOM.ShowFakeShadow(false);

		//// Allow self shadowing (note: this only works in conjunction with the shaders defined above)
		//sm->setShadowTextureSelfShadow(true);
		//// Set the caster material which uses the shaders defined above
		//sm->setShadowTextureCasterMaterial("Ogre/DepthShadowmap/Caster/Float");
		//// Set the pixel format to floating point
		//sm->setShadowTexturePixelFormat(Ogre::PF_FLOAT32_R);
		//// You can switch this on or off, I suggest you try both and see which works best for you
		//sm->setShadowCasterRenderBackFaces(false);
		//// Finally enable the shadows using texture additive integrated
		//sm->setShadowTechnique(Ogre::SHADOWTYPE_TEXTURE_MODULATIVE_INTEGRATED);


		//Shadow
		//_smOgre->setShadowTechnique(Ogre::SHADOWTYPE_TEXTURE_ADDITIVE_INTEGRATED);
		//_smOgre->setShadowTechnique(Ogre::SHADOWTYPE_STENCIL_MODULATIVE);
		_smOgre->setShadowTechnique(Ogre::SHADOWTYPE_TEXTURE_MODULATIVE);
		//_smOgre->setShadowUseInfIniteFarPlane(false);
		//_smOgre->setShadowTexturePixelFormat(Ogre::PF_FLOAT16_RGB); 
		_smOgre->setShadowFarDistance(1000.0); 
		//_smOgre->setShadowColour(Ogre::ColourValue(0.3, 0.3, 0.3));
		//_smOgre->setShadowUseInfIniteFarPlane(false);
		//_smOgre->setShadowTextureSelfShadow(true);
		//_smOgre->setShadowTextureCasterMaterial("ShadowCasterMat");
		//_smOgre->setShadowCasterRenderBackFaces(false);
		//_smOgre->setShadowTextureFadeEnd(0.3); 
		//_smOgre->setShowDebugShadows(true);
		_smOgre->setShadowTextureSettings(1024, 1);
		//_smOgre->setShadowTextureConfig(1, 1024,1024, Ogre::PF_FLOAT16_RGB);
		//_smOgre->setShadowCasterRenderBackFaces(true); 

		//const unsigned numShadowRTTs = sm->getShadowTextureCount(); 
		//for (unsigned i = 0; i < numShadowRTTs; ++i) { 
		//	Ogre::TexturePtr tex = sm->getShadowTexture(i); 
		//	Ogre::Viewport *vp = tex->getBuffer()->getRenderTarget()->getViewport(0); 
		//	vp->setBackgroundColour(Ogre::ColourValue(1, 1, 1, 1)); 
		//	vp->setClearEveryFrame(true); 
		//	Ogre::CompositorManager::getSingleton().addCompositor(vp, "Blur"); 
		//	Ogre::CompositorManager::getSingleton().setCompositorEnabled(vp, "Blur", true); 
		//} 

		////下面是四种阴影的优化方案
		//cameraSetup = ShadowCameraSetupPtr(OGRE_NEW PlaneOptimalShadowCameraSetup(mPlane));
		Ogre::ShadowCameraSetupPtr cameraSetup;
		//mLiSPSMSetup->setUseAggressiveFocusRegion(false);

		//cameraSetup = ShadowCameraSetupPtr(OGRE_NEW DefaultShadowCameraSetup());
		Ogre::FocusedShadowCameraSetup* FSC = OGRE_NEW Ogre::FocusedShadowCameraSetup();
		cameraSetup = Ogre::ShadowCameraSetupPtr(FSC);
		FSC->setUseAggressiveFocusRegion(true);

		//Ogre::LiSPSMShadowCameraSetup* mLiSPSMSetup = OGRE_NEW Ogre::LiSPSMShadowCameraSetup();
		//cameraSetup = Ogre::ShadowCameraSetupPtr(mLiSPSMSetup);
		//mLiSPSMSetup->setOptimalAdjustFactor(0.5f); 
		//mLiSPSMSetup->setUseAggressiveFocusRegion(true); 
		//mLiSPSMSetup->setUseSimpleOptimalAdjust(true); 
		_smOgre->setShadowCameraSetup(cameraSetup);
	}

};//GLL


#endif GLL_OGRE
